package com.ruan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.entity.R;
import com.ruan.entity.SpecKey;
import com.ruan.entity.SpecOptions;
import com.ruan.mapper.SpecKeyMapper;
import com.ruan.service.SpecOptionsService;
import com.ruan.mapper.SpecOptionsMapper;
import com.ruan.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
* @author 59499
* @description 针对表【spec_options】的数据库操作Service实现
* @createDate 2023-09-10 00:37:27
*/
@Service
public class SpecOptionsServiceImpl extends ServiceImpl<SpecOptionsMapper, SpecOptions>
    implements SpecOptionsService{

    @Autowired
    private SpecOptionsMapper specOptionsMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SpecKeyMapper specKeyMapper;

    @Override
    public R<Void> addSpecOptions(String title, Integer specKeyId,Integer categoryId) {
        QueryWrapper<SpecOptions> wrapper = new QueryWrapper<>();
        wrapper.eq("title",title);
        wrapper.eq("spec_key_id",specKeyId);
        wrapper.eq("status",1);
        List<SpecOptions> specOptionsList = specOptionsMapper.selectList(wrapper);
        if (!specOptionsList.isEmpty()){
            return new R<>(100,"该规格选项已存在");
        }
        SpecOptions specOptions = new SpecOptions();
        specOptions.setTitle(title);
        specOptions.setSpecKeyId(specKeyId);
        int row = specOptionsMapper.insert(specOptions);
        if (row != 1){
            return new R<>(100,"添加失败");
        }
        return new R<>(200,"添加成功");
    }

    @Override
    public R<List<SpecOptions>> getSpecOptionsByKeyId(Integer specKeyId) {
        QueryWrapper<SpecOptions> wrapper = new QueryWrapper<>();
        wrapper.eq("spec_key_id",specKeyId);
        wrapper.eq("status",1);
        List<SpecOptions> specOptionsList1 = specOptionsMapper.selectList(wrapper);
        return new R<>(200,"查询成功",specOptionsList1);
    }

    @Override
    @Transactional
    public R<Void> deleteSpecOptionById(Integer id) {
        SpecOptions specOptions = new SpecOptions();
        specOptions.setId(id);
        specOptions.setStatus(0);
        int row = specOptionsMapper.updateById(specOptions);
        if (row != 1){
            return new R<>(100,"删除失败");
        }
        SpecOptions specOptions1 = specOptionsMapper.selectById(id);
        SpecKey specKey = specKeyMapper.selectById(specOptions1.getSpecKeyId());
        return new R<>(200,"删除成功");
    }
}




